<?php
/*
Plugin Name: Archive-Sitemap-8dianpu
Plugin URI: http://www.wzfb.info/
Description: WP Archive-Sitemap Generator plugin Generates simple Archives/Sitemap based on your posts and pages. This is not another XML sitemap plugin, but rather a nice sitemap generator. Just add <code>&lt;!--wp-archive-sitemap-generator--&gt;</code> to page or posts under HTML section (please see readme.txt file for usage/instruction). Admin panel <code>(Settings -> Archive-Sitemap)</code> also available for various options, i.e. sort posts, show pages, show comments, number of posts/page.
Version: 1.0
Author: 8dianpu
Author URI: http://www.8dianpu.com/blog/
/*
    Copyright (C) 2009-10 Arpit Shah.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


require_once "Paginated.php";
require_once "DoubleBarLayout.php";

//To replace the <!--wp-archive-sitemap-generator--> with the actual sitemap
function wp_archive_sitemap_generator($text) {
	global $wpdb, $table_prefix;

	//If this text included in pages or posts? <!--wp-archive-sitemap-generator-->
	if (preg_match("|<!--wp-archive-sitemap-generator-->|", $text)) {

		//Here you can get option values
		$wp_simple_order = get_option('wp_archive_sitemap_generator_order');
		$wp_page_checked = get_option('wp_archive_sitemap_generator_page');
		$wp_comment_checked = get_option('wp_archive_sitemap_generator_comment');
		$wp_post_count = get_option('wp_archive_sitemap_generator_post_count');
		$wp_sitemap_show_sponsor_link = get_option('wp_archive_sitemap_generator_sponsor');

		//do the order by
		switch ($wp_simple_order) {
			case 'sort_desc':
				$sqlorder = "ORDER BY post_date DESC";
				break;
			case 'sort_asc':
				$sqlorder = "ORDER BY post_date";
				break;
			case 'sort_abc':
				$sqlorder = "ORDER BY post_title";
				break;
			case 'sort_xyz':
				$sqlorder = "ORDER BY post_title DESC";
				break;
		}

		$sqlwhere = "WHERE post_type='post' AND post_status='publish' ";

		$sql = "SELECT * FROM " . $table_prefix . "posts " . $sqlwhere . $sqlorder;

		$allposts = $wpdb->get_results($sql);

		$postcounter = 0;
		$postheader = "BLOG SITEMAP:";
		$postend = "WP Archive-Sitemap Generator";
		
		foreach($allposts as $ap)
		{
			$perma = get_permalink($ap->ID);
			$postcounter++;
			if($postcounter%3==1){
				$posts = '<div class="dir-left"><a href=' . $perma . '>' . $ap->post_title . '</a></div>';
			}
			else if($postcounter%3==2){
				$posts = '<div class="dir-center"><a href=' . $perma . '>' . $ap->post_title . '</a></div>';
			}	
			else{
				$posts = '<div class="dir-right"><a href=' . $perma . '>' . $ap->post_title . '</a></div>';
			}
			$arrayposts[] = $posts;
		}

		$posts = '<b><i>'.'Posts (' . $postcounter . ') &nbsp; </i></b><br/>';

		if (!($wp_page_checked == 'on'))
		{
			$posts = '<br />'.$posts;
		}
		//show pages
		$pagecounter = 0;
		if ($wp_page_checked == 'on') {

			$sqlpage = "SELECT * FROM " . $table_prefix . "posts where post_type='page' AND post_status='publish'";
			$allpages = $wpdb->get_results($sqlpage);

			foreach($allpages as $ap) {
				$perma = get_permalink($ap->ID);
				$pagecounter++;

				$pages .= '&nbsp;&nbsp;&nbsp;<b>' . $pagecounter . '.</b>&nbsp;&nbsp;' . '<a href=' . $perma . '>' . $ap->post_title . '</a><br/>';
			}
			$pages = '<b><i><br />2222222'.'Pages (' . $pagecounter . ') &nbsp;</i></b><br/>' .$pages;
		}
		
		//pagination (100 posts per page)
		$page = $_GET['page'];
		$pagedResults = new Paginated($arrayposts, $wp_post_count, $page);
		while($row = $pagedResults->fetchPagedRow()) {	//when $row is false loop terminates
			$row1 .= $row;
		}

		//important to set the strategy to be used before a call to fetchPagedNavigation
		$pagedResults->setLayout(new DoubleBarLayout());
		$pagedResult1 = $pagedResults->fetchPagedNavigation();

		$pluginlink = "http://wordpress.org/extend/plugins/wp-archive-sitemap-generator/";
		$target1 = "_blank";


		if($wp_sitemap_show_sponsor_link == 'on')
		{
			$sponsor_link1 = "";
		}
		else
		{
			$sponsor_link1 = '<font size="1"><br/><i> Generated by: <a href="http://www.8dianpu.com/blog" target="_blank">赢在网络</a></i></font>';
		}

		$text = preg_replace("|<!--wp-archive-sitemap-generator-->|", $row1 . '<br />' . $pagedResult1 . $sponsor_link1 , $text);
	}

	return $text;

} //end wp_archive_sitemap_generator()


//admin menu
function wp_archive_sitemap_generator_admin() {
	if (function_exists('add_options_page')) {
		add_options_page('wp-archive-sitemap-generator', 'Archive-Sitemap', 1, basename(__FILE__), 'wp_archive_sitemap_generator_admin_panel');
  }
}

function wp_archive_sitemap_generator_admin_panel() {

	//If this is first time running add default option
	add_option('wp_archive_sitemap_generator_order', 'sort_desc', 'WP Archive-Sitemap Generator Plugin');
	add_option('wp_archive_sitemap_generator_page', 'false', 'WP Archive-Sitemap Generator Plugin');
	add_option('wp_archive_sitemap_generator_comment', 'false', 'WP Archive-Sitemap Generator Plugin');
	add_option('wp_archive_sitemap_generator_post_count', '100', 'WP Archive-Sitemap Generator Plugin');

	add_option('wp_archive_sitemap_generator_sponsor', 'false', 'WP Archive-Sitemap Generator Plugin');

	//get posted options
	$wp_simple_order = $_POST['wp_simple_order'];
	$wp_post_count = $_POST['wp_post_count'];

	if (isset($_POST['option_value'])) {
		//update settings
		$wp_simple_order = $_POST['wp_simple_order'];
		$wp_page_checked = $_POST['wp_page_checked'];
		$wp_post_count = $_POST['wp_post_count'];
		$wp_comment_checked = $_POST['wp_comment_checked'];

		$wp_sitemap_show_sponsor_link = $_POST['wp_sitemap_show_sponsor_link'];

		update_option('wp_archive_sitemap_generator_order', $wp_simple_order);
		update_option('wp_archive_sitemap_generator_page', $wp_page_checked);
		update_option('wp_archive_sitemap_generator_comment', $wp_comment_checked);
		update_option('wp_archive_sitemap_generator_post_count', $wp_post_count);

		update_option('wp_archive_sitemap_generator_sponsor', $wp_sitemap_show_sponsor_link);

		echo '<div id="message" class="updated fade"><p><strong>Settings saved.</strong></p></div>';

	} else {
		$wp_simple_order = get_option('wp_archive_sitemap_generator_order');
		$wp_page_checked	= get_option('wp_archive_sitemap_generator_page');
		$wp_comment_checked	= get_option('wp_archive_sitemap_generator_comment');
		$wp_post_count	= get_option('wp_archive_sitemap_generator_post_count');

		$wp_sitemap_show_sponsor_link = get_option('wp_archive_sitemap_generator_sponsor');

	}

	?>

<style type="text/css">
	body {
		font-family: Verdana;
		font-size: 13px;
	}

	a {
		text-decoration: none;
	}

	a:hover {
		text-decoration: underline;
	}
</style>

	<div class=wrap>
		<form method="post">

		<h2>WP Archive-Sitemap-8dianpu</h2>


		<div id="poststuff" class="metabox-holder has-right-sidebar">

		<div style="float:left;width:60%;">


			<div class="postbox">
				<h3>Sitemap Options</h3>
				<div>

					<table class="form-table">
					<tr valign="top" class="alternate">
						<th scope="row"><label>Post Sorting Order</label></th>
						<td>
							<input name="wp_simple_order" type="radio" value="sort_desc" <?php checked('sort_desc', $wp_simple_order); ?> class="tog"/>
					By post date (newest first) (default)

						</td>
					</tr>
					<tr valign="top">
						<th scope="row"></th>
						<td>
							<input name="wp_simple_order" type="radio" value="sort_asc" <?php checked('sort_asc', $wp_simple_order); ?> class="tog"/>
						By post date (oldest first)

						</td>
					</tr>
					<tr valign="top" class="alternate">
						<th scope="row"></th>
						<td>
							<input name="wp_simple_order" type="radio" value="sort_abc" <?php checked('sort_abc', $wp_simple_order); ?> class="tog"/>
						By Title (A to Z order)

						</td>
					</tr>
					<tr valign="top">
						<th scope="row"></th>
						<td>
							<input name="wp_simple_order" type="radio" value="sort_xyz" <?php checked('sort_xyz', $wp_simple_order); ?> class="tog"/>
						By Title (Z to A order)

						</td>
					</tr>

					<tr valign="top" class="alternate">
						<th scope="row"><label>Include Pages?</label></th>
						<td>
							<input name="wp_page_checked" type="checkbox" <?php checked('on', $wp_page_checked); ?> class="tog"/>
							Show pages (default unchecked)
						</td>
					</tr>

					<tr valign="top">
						<th scope="row"><label>Number of posts per page</label></th>
						<td>
							<input name="wp_post_count" type="radio" value="50" <?php checked('50', $wp_post_count); ?> class="tog"/>
						50 posts/page

						</td>
					</tr>
					<tr valign="top" class="alternate">
						<th scope="row"></th>
						<td>
							<input name="wp_post_count" type="radio" value="100" <?php checked('100', $wp_post_count); ?> class="tog"/>
						100 posts/page (default)

						</td>
					</tr>
					<tr valign="top">
						<th scope="row"></th>
						<td>
							<input name="wp_post_count" type="radio" value="150" <?php checked('150', $wp_post_count); ?> class="tog"/>
						150 posts/page

						</td>
					</tr>
					<tr valign="top" class="alternate">
						<th scope="row"></th>
						<td>
							<input name="wp_post_count" type="radio" value="9999" <?php checked('9999', $wp_post_count); ?> class="tog"/>
						9999 posts/page

						</td>
					</tr>

					<tr valign="top">
						<th scope="row"><label>Show Comments?</label></th>
						<td>
							<input name="wp_comment_checked" type="checkbox" <?php checked('on', $wp_comment_checked); ?> class="tog"/>
							Show comemnts (default unchecked)
						</td>
					</tr>

					<tr valign="top" class="alternate">
						<th scope="row"><label>Show "Generated by.." link after sitemap?</label></th>
						<td>
							<input name="wp_sitemap_show_sponsor_link" type="checkbox" <?php checked('on', $wp_sitemap_show_sponsor_link); ?> class="tog"/>
							<code>Check</code> to hide
						</td>
					</tr>


					</table>
				</div>
			</div>



			<div class="submit">
				<input type="submit" name="option_value" class="button-primary" value="<?php _e( 'Update Options' ); ?>" />
			</div>

		</form>
</div>
		<div id="side-info-column" class="inner-sidebar">
			<div class="postbox">
			  <h3 class="hndle"><span>WP Archive-Sitemap Generator by Arpit Shah</span></h3>
			  <div class="inside">
				<ul>
				<li><a href="http://wordpress.org/extend/plugins/wp-archive-sitemap-generator/" title="Twitter Goodies" target="_blank">Plugin Homepage</a></li>
				<li><a href="http://arpitshah.com" title="Visit Arpit's Personal Site" target="_blank">Arpit's Website</a></li>
				</ul>
			  </div>
			</div>
	     </div>
			<br>
			<div id="side-info-column" class="inner-sidebar">
				<div class="postbox">
				  <h3 class="hndle"><span>My Other Plugins</span></h3>
				  <div class="inside">
					<ul>
					<li><a href="http://wordpress.org/extend/plugins/wp-google-buzz/" title="WP Google-buzz" target="_blank">WP Google-buzz</a></li>
					<li><a href="http://wordpress.org/extend/plugins/all-in-one-webmaster/" title="All in One Webmaster" target="_blank">All in One Webmaster</a></li>
					<li><a href="http://wordpress.org/extend/plugins/twitter-goodies/" title="Twitter Goodies" target="_blank">Twitter Goodies</a></li>
					</ul>
				  </div>
				</div>
		     </div>
			 <br>
			  <div id="side-info-column" class="inner-sidebar">
				<div class="postbox">
				  <h3 class="hndle"><span>Thanks for your support</span></h3>
				  <div class="inside">
					<ul>
					If you like this plugin and find it useful, help keep this plugin free and actively developed by clicking the donate button.<br><br>
					<form action="https://www.paypal.com/cgi-bin/webscr" target="_blank" method="post">
					<input type="hidden" name="cmd" value="_s-xclick">
					<input type="hidden" name="hosted_button_id" value="10641755">
					<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
					<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
					</form>

					</ul>
				  </div>
				</div>
			</div>

		</div>
	</div><?php
}


//hooks
add_filter('the_content', 'wp_archive_sitemap_generator', 2);
add_action('admin_menu', 'wp_archive_sitemap_generator_admin');

?>